package com.feibi.firstplugin;

import com.feibi.firstplugin.Bean.DuText;
import com.feibi.firstplugin.Bean.ProjectsResult;
import com.feibi.firstplugin.Bean.WordData;
import com.feibi.firstplugin.Bean.YiYanData;
import com.feibi.firstplugin.Utils.*;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class ContentUtil {

    public static void getDu(StringCallback callback) {
        HttpUtil.getInstance().requestGet("https://api.shadiao.pro/du", null, null, new HttpCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                Gson gson = new Gson();
                DuText duText = gson.fromJson(result, DuText.class);
                callback.onResult(duText.getData().getText());
            }

            @Override
            public void onReqFail(String result) {
//                NotifyUtil.notify(result);
            }
        });
    }

    public static void getYiYan(StringCallback callback) {
        HashMap<String, Object> head = new HashMap<>();
        head.put("token", "GIF0N5YCMmNWQm");
        HttpUtil.getInstance().requestGet("http://api.txapi.cn/v1/hitokoto", null, head, new HttpCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                Gson gson = new Gson();
                YiYanData yiYanData = gson.fromJson(result, YiYanData.class);
                if (yiYanData.getCode() == 200)
                    callback.onResult(yiYanData.getData().getContent());
                else
                    callback.onResult(yiYanData.getMsg());
            }

            @Override
            public void onReqFail(String result) {
                callback.onResult(result);
//                NotifyUtil.notify(result);
            }
        });
    }

    public static void getWords(String projectType, String projectId, ResultCallback callback) {
        HttpUtil.getInstance().requestGet("https://ssl.feibi.site:30017/oa/OaCnToTw/list_overt?pageNum=1&pageSize=10000&projectType=" + projectType + "&projectId=" + projectId, null, null, new HttpCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                Gson gson = new Gson();
                WordData wordData = gson.fromJson(result, WordData.class);
                if (wordData.getCode() == 200)
                    callback.onResult(wordData.getData().getResult());
                else
                    NotifyUtil.notify("获取数据失败：\n" + wordData.getMsg());
            }

            @Override
            public void onReqFail(String result) {
                NotifyUtil.notify("获取数据失败：\n" + result);
            }
        });
    }

    public static void initWords() {
        HttpUtil.getInstance().requestGet("https://ssl.feibi.site:30017/oa/OaCnToTw/list_overt?pageNum=1&pageSize=10000&projectType=0", null, null, new HttpCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                Gson gson = new Gson();
                WordData wordData = gson.fromJson(result, WordData.class);
                Global.normalWordResults = wordData.getData().getResult();
                System.out.println("成功初始化通用word");
            }

            @Override
            public void onReqFail(String result) {
            }
        });
    }


    public static void getProjects() {
        String json = "[\n" +
                "                    {\n" +
                "                        \"dictType\": \"sys_cn_to_tw_project\"\n" +
                "                    }\n" +
                "                ]";
        HttpUtil.getInstance().requestPost("https://ssl.feibi.site:30017/system/dict/data/types", json, null, new HttpCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                Gson gson = new Gson();
                ProjectsResult res = gson.fromJson(result, ProjectsResult.class);
                if (res.getData().size() > 0) {
                    Global.projectLists = res.getData().get(0).getList();
                    System.out.println("成功初始化项目列表");
                }
            }

            @Override
            public void onReqFail(String result) {

            }
        });
    }
}
